﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CarLoanSystem.Entity;

namespace CarLoanSystem.DAL
{
    public class CarModelInfoDAL : DBBase
    {
        public static CarModelItem LoadCarModel(int id)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [CAR_MODEL_ID],[CAR_BRAND_ID],[CAR_MODEL_NAME]
                FROM [CAR_LOAN].[dbo].[CAR_MODEL] where [CAR_MODEL_ID] = @CAR_MODEL_ID", DBBase.con);
                command.Parameters.AddWithValue("@CAR_MODEL_ID", id);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    CarModelItem item = new CarModelItem();
                    item.CAR_MODEL_ID = Convert.ToInt32(dt.Rows[0]["CAR_MODEL_ID"].ToString());
                    item.CAR_BRAND_ID = Convert.ToInt32(dt.Rows[0]["CAR_BRAND_ID"].ToString());
                    item.CAR_MODEL_NAME = dt.Rows[0]["CAR_MODEL_NAME"].ToString();
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllCarModel(string id = "", string modelName = "" , string brandName = "")
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                string query = @"SELECT dbo.CAR_MODEL.CAR_MODEL_ID, dbo.CAR_MODEL.CAR_BRAND_ID, dbo.CAR_MODEL.CAR_MODEL_NAME, dbo.CAR_BRAND.CAR_BRAND_NAME
                                        FROM dbo.CAR_BRAND INNER JOIN
                      dbo.CAR_MODEL ON dbo.CAR_BRAND.CAR_BRAND_ID = dbo.CAR_MODEL.CAR_BRAND_ID where 1=1";
                if (id != "")
                {
                    query += " And CAR_MODEL_ID = " + id;
                }
                if (modelName != "")
                {
                    query += " And CAR_MODEL_NAME like '%" + modelName + "%'";
                }
                if (brandName != "")
                {
                    query += " And CAR_BRAND_NAME like '%" + brandName + "%'";
                }
                SqlCommand command = new SqlCommand(query, DBBase.con);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllForComboBox(int id)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                string query = @"SELECT [CAR_MODEL_ID] ,[CAR_BRAND_ID] ,[CAR_MODEL_NAME] FROM [CAR_LOAN].[dbo].[CAR_MODEL] where CAR_BRAND_ID = " + id;

                SqlCommand command = new SqlCommand(query, DBBase.con);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static void Insert(CarModelItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[CAR_MODEL]
                                                       ([CAR_BRAND_ID]
                                                       ,[CAR_MODEL_NAME])
                                                 VALUES
                                                      ( @CAR_BRAND_ID
                                                        ,@CAR_MODEL_NAME)", DBBase.con);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", item.CAR_BRAND_ID);
                command.Parameters.AddWithValue("@CAR_MODEL_NAME", item.CAR_MODEL_NAME);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static bool Update(CarModelItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"UPDATE [CAR_LOAN].[dbo].[CAR_MODEL]
                                                        SET [CAR_BRAND_ID] = @CAR_BRAND_ID
                                                            ,[CAR_MODEL_NAME] = @CAR_MODEL_NAME
                                                    WHERE ([CAR_MODEL_ID] = @CAR_MODEL_ID )", DBBase.con);

                command.Parameters.AddWithValue("@CAR_MODEL_NAME", item.CAR_MODEL_NAME);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", item.CAR_BRAND_ID);
                command.Parameters.AddWithValue("@CAR_MODEL_ID", item.CAR_MODEL_ID);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
                return true;
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }
    }
}
